package UIFree.effects
{
	import UIFree.ObjectEvent;
	import UIFree.effects.flames.ef_flame;
	import UIFree.uif_ecomponent;
	
	import flash.display.BlendMode;
	import flash.events.Event;
	import flash.geom.Rectangle;

	public class ef_flames extends uif_effect {
		private var _requireStop: Boolean;
		
		public static const CIRCLETYPE	: int = 1;
		public static const SQUARETYPE	: int = 2;
		public static const UPDATEFLAME	: String = 'UPDATEFLAME';
		
		private var _flameLayer: uif_ecomponent;
		
		public function ef_flames(parent: uif_ecomponent, styleClass: String='', masked: Boolean = false) {
			super(parent, styleClass);
			var inner: Rectangle = parent.getInner();
			_flameLayer = new uif_ecomponent(this, inner.x, inner.y, inner.width, inner.height);
			if (masked) _flameLayer.mask = new uif_ecomponent(this, inner.x, inner.y, inner.width, inner.height, 'white');
		}
		
		override protected function defaultOptions(): Object {
			var inner: Rectangle = parentComponent.getInner();
			return {
				flameStyle		: 'red',
				flameSize		: inner.size.length / 30,
				flameVariation	: 0.5,
				flameClass		: ef_flame,
				flameLive		: 0,
				flameType		: ef_flame.CYRCLEFLAME,
				flameAtack		: 0.1,
				flameFade		: 0.1,
				blendMode		: BlendMode.NORMAL,
				density			: 0.6
			};
		}
		
		override public function requireStop(): void {
			dispatchEvent(new Event(REQUIRESTOPEFFECT));
			_requireStop = true;
		}
		
		override protected function doStart(): void {
			super.doStart();
			_requireStop = false;
		}
		
		override protected function doStop(): void {
			super.doStop();
			_flameLayer.removeChilds();
		}
		
		protected function initFlame(): void {
			var inner		: Rectangle = _flameLayer.getInner();
			var curRadius	: Number = options.flameSize + options.flameSize * Math.random() * options.flameVariation;
			var flame		: ef_flame = new options.flameClass(_flameLayer, inner.width * Math.random(), inner.height * Math.random(), curRadius, curRadius, options.flameType, options.flameStyle);
			flame.alpha = 0;
			flame._data = {
				frame: 0
			}
			flame.blendMode = options.blendMode; 
		}
		
		public function get flameLayer(): uif_ecomponent {
			return _flameLayer;
		}
		
		protected function doFlameStep(params: Object): void {
			dispatchEvent(new ObjectEvent(UPDATEFLAME, params));
		}
		
		override protected function update(a_frame: int): void {
			if (!_requireStop && (Math.random() > 1 - options.density)) initFlame();
			if (flameLayer.numChildren > 0) {  
				for (var i: int=0; i<flameLayer.numChildren; i++) {
					var l_flame	: ef_flame = flameLayer.getChildAt(i) as ef_flame;
/*					var mat	: Matrix = new Matrix();
					mat.rotate(options.flameRoll);
					mat.scale(options.flameVolumeChange, options.flameVolumeChange);
					mat.translate(options.flameSpeed[0], options.flameSpeed[1]);
					mat.concat(flame.transform.matrix);
					flame.transform.matrix = mat;*/
					
					if ((options.flameLive) && (l_flame._data.frame > options.flameLive)) {
						l_flame.alpha -= options.flameFade;
						if (l_flame.alpha <= 0) {
							l_flame.dispose();
							continue;
						}
					} else {
						var	atackLength: Number = l_flame._data.frame * options.flameAtack;
						if (atackLength <= 1) l_flame.alpha = atackLength;
					}
					doFlameStep({
						index	: i,
						flames	: this,
						frame	: a_frame,
						options	: options,
						flame	: l_flame
					});
					l_flame._data.frame += 1;
				}
				super.update(a_frame);
			} else if (_requireStop) stop();
		}
		
	}
}